package com.zhss.eshop.order.price.coupon;

import com.alibaba.fastjson.JSONObject;
import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.promotion.domain.dto.PromotionCouponDTO;
import org.springframework.stereotype.Component;

/**
 * 现金券抵扣金额计算组件
 * @author suyuan
 *
 */
@Component
public class CashCouponCalculator implements CouponCalculator {

//	@Autowired
//	private JsonExtractor jsonExtractor;

	@Override
	public Double calculate(OrderInfoDTO order, PromotionCouponDTO coupon) throws Exception {
		JSONObject rule = JSONObject.parseObject(coupon.getRule());
		Double discountAmount = rule.getDouble( "discountAmount");
		Double payableAmount = order.getPayableAmount();

		if(discountAmount > payableAmount) {
			return payableAmount;
		}

		return discountAmount;
	}

}
